package com.own.core.main;

import java.util.Map;

import javax.persistence.Transient;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.model.Model;

import com.own.core.app.OwnSession;
import com.own.core.function.add.AddObjectPanel;
import com.own.core.html.component.link.OwnAjaxLink;
import com.own.core.html.component.link.OwnLink;
import com.own.core.html.form.Label.OwnLabel;
import com.own.core.html.panel.OwnPanel;
import com.own.core.object.Dao;
import com.own.core.object.admin.User;
import com.own.util.ResourceUtil;

public class HeaderPanel extends OwnPanel {
	@Transient
	OwnLabel titleLabel;
	@Transient
	OwnLink signOut;
	@Transient
	OwnAjaxLink perferenceLink;

	public HeaderPanel() {

		super("header");

		String userName = "";
		final User user = ((OwnSession) getSession()).getUser();
		if (user != null) {
			userName = user.getFirstName() + " " + user.getLastName();

		}
		titleLabel = new OwnLabel("loginUserName", userName);
		add(titleLabel);
		
		// add perference link
		perferenceLink = new OwnAjaxLink("UserPerference") {
			public void onClick(final AjaxRequestTarget target) {
				AddObjectPanel daoPanel = new AddObjectPanel(HeaderPanel.this) {

					@Override
					protected void onBeforeRender() {
						super.onBeforeRender();
						try {
							getComponentMap().get("userName").setEnabled(false);
							getComponentMap().get("userName").add(
									new AttributeModifier("class", true,
											new Model("disable")));
							getComponentMap().get("active").setEnabled(false);
							getComponentMap().get("active").add(
									new AttributeModifier("class", true,
											new Model("disable")));
						} catch (Exception e) {

						}
					}
				};
				daoPanel.draw((Dao) user);

				getModalWindow().setContent(daoPanel);
				getModalWindow().show(target);
				// componentMap=daoPanel.getComponentMap();
				//

				// setMainComponent(HeaderPanel.this);

			}

		};
		add(perferenceLink);
		
		//logout link
		signOut = new OwnLink("logout") {

			@Override
			public void onClick() {
				this.getSession().invalidate();
				setResponsePage(this.getApplication().getHomePage());

			}
		};
		add(signOut);

	}

}
